package org.pgis.gui.tbl;

import java.awt.Color;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;

import org.pgis.entity.ConnectingLine;

public class InteractiveTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	public static final int COLOR = -1;
    public static final int START_POINT = 0;
    public static final int END_POINT = 1;
    public static final int DISTANCE = 2;

    protected String[] columnNames;
    protected Vector<ConnectingLine> dataVector;

    public InteractiveTableModel(String[] columnNames) {
        this.columnNames = columnNames;

        dataVector = new Vector<ConnectingLine>();

        /*ConnectingLine cl = new ConnectingLine();
        cl.setLineColor(Color.red);
        cl.setStartPoint(new Point3d(2,4,5));
        cl.setEndPoint(new Point3d(1,2,6));

        dataVector.add(cl);*/
    }

    public void addLineToTable(ConnectingLine cl){
    	dataVector.add(cl);
    }

    public void setConnectingLinesToTable(Vector<ConnectingLine> c){
    	this.dataVector = c;
    }

    public String getColumnName(int column) {
        return columnNames[column];
    }

    public boolean isCellEditable(int row, int column) {
        return false;
    }

    public Class getColumnClass(int column) {
        switch (column) {
            case COLOR:
            	return Color.class;
            case START_POINT:
            case END_POINT:
            	return String.class;
            case DISTANCE :
            	return Double.class;
            default:
            	return Object.class;
        }
    }

    public Object getValueAt(int row, int column) {
        ConnectingLine record = (ConnectingLine)dataVector.get(row);
        switch (column) {
            case COLOR:
               return record.getLineColor();
            case START_POINT:
               return record.getStartPoint();
            case END_POINT:
               return record.getEndPoint();
            case DISTANCE :
            	return record.getDistance();
            default:
               return new Object();
        }
    }

    public void setValueAt(Object value, int row, int column) {
        ConnectingLine record = (ConnectingLine)dataVector.get(row);
        switch (column) {
            case COLOR:
               record.setLineColor(record.getLineColor());
               break;
            case START_POINT:
               record.setStartPoint(record.getStartPoint());
               break;
            case END_POINT:
               record.setEndPoint(record.getEndPoint());
               break;
            default:
               System.out.println("invalid index");
        }
        fireTableCellUpdated(row, column);
    }

    public int getRowCount() {
        return dataVector.size();
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public boolean hasEmptyRow() {
        if (dataVector.size() == 0) return false;
        ///ConnectingLine audioRecord = (ConnectingLine)dataVector.get(dataVector.size() - 1);
        return false;
    }

    public ConnectingLine getConnectingLineByInded(int index) {
    	return dataVector.get(index);
    }

    /*  public void addEmptyRow() {
        dataVector.add(new AudioRecord());
        fireTableRowsInserted(
           dataVector.size() - 1,
           dataVector.size() - 1);
    }*/
}